import {fetchCourse,fetchCourseDetail} from '@/api/course.js'
import * as types from '@/store/action-types.js'
import {Toast} from 'cube-ui';
export default{
    namespaced: true,
    state:{
        courseList:[],
        // cartList:JSON.parse(localStorage.getItem('vuex'))||[]
        cartList:[]
    },
    getters:{

    },
    mutations:{
        setCourse(state,result){
            state.courseList = result;
        },
        [types.ADD_CARLIST](state,course){
            let flag = state.cartList.every((item,index)=>{
                return item.id!=course.id
            });
            if(flag){
               state.cartList.push(course); 
               Toast.$create({
                   txt:'添加购物车成功',
                   time:700,
                   type:'text'
               }).show()
            }else{
                Toast.$create({
                    txt: '请勿重复添加',
                    time: 700,
                    type: "text"
                  }).show(); 
            }
        },
        [types.EMPTY_CARLIST](state){
            state.cartList = [];
        },
        [types.REMOVE_CARLIST](state,courseId){
            state.cartList = state.cartList.filter(item=>item.id!=courseId);
        }
    },
    actions:{
        // 获取课程
        async setCourse({commit}){
            let result = await fetchCourse();
            commit('setCourse',result)
        }
    }
}